package com.example.module_clocking

import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.utils.RouterPath
import com.example.module_clocking.databinding.ActivityClockingBinding
import com.example.module_clocking.model.ClockingBody
import com.example.module_clocking.viewModel.ClockingViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Timer
import kotlin.concurrent.timerTask

@AndroidEntryPoint
@Route(path = RouterPath.CLOCKING_UI)
class ClockingActivity : BaseActivity<ClockingViewModel, ActivityClockingBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_clocking
    private val mViewModel: ClockingViewModel by viewModels()
    var simpleDateFormat = SimpleDateFormat("HH:mm:ss")
    var sdf = SimpleDateFormat("yyyy年MM月dd日 EE")
    var sdftoM = SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss")
    var sdftoH = SimpleDateFormat("HH")
    var new = System.currentTimeMillis()
    var timer: Timer? = null


    override fun initData() {
        super.initData()

    }

    override fun initView() {
        super.initView()
        mBinding.clockingTime1.text = simpleDateFormat.format(System.currentTimeMillis())

        timer = Timer()
        lifecycleScope.launch {
            timer!!.schedule(timerTask {
                new = System.currentTimeMillis()
                lifecycleScope.launch {
                    mBinding.clockingTime1.setText(simpleDateFormat.format(new))
                    mBinding.clockingTime2.setText(sdf.format(new))
                }
            }, 0, 1000)
        }
        if (System.currentTimeMillis() - SPUtils.getInstance()
                .getLong("time") > 24 * 60 * 60 * 1000
        ) {
            SPUtils.getInstance().put("Dk",true)
            SPUtils.getInstance().put("shangwu", true)
            SPUtils.getInstance().put("xiawu", true)
        }
        if (sdftoH.format(System.currentTimeMillis()) > "12") {
            mBinding.tvDk.text = "下午打卡"
            mBinding.tvDkTime.text = "13:00-20:00"
        } else {
            mBinding.tvDk.text = "上午打卡"
            mBinding.tvDkTime.text = "9:00-12:00"
        }
        if (SPUtils.getInstance().getBoolean("Dk")===false){
            mBinding.tvDkBtn.text = "已下班"
            mBinding.btnDk.setBackgroundResource(R.drawable.shape_dk_bg_not)
        }else{
            mBinding.btnDk.setOnClickListener {

                if (SPUtils.getInstance().getBoolean("shangwu", true)) {
                    mBinding.tvDkBtn.text = "下班"
                    SPUtils.getInstance().put("shangwu",false)
                    SPUtils.getInstance().put("swsb",System.currentTimeMillis())
                    mViewModel.clocking(
                        ClockingBody(
                            user = 0,
                            status = 1,
                            morningCheckIn = sdftoM.format(SPUtils.getInstance().getLong("swsb")),
                            afternoonCheckIn = sdftoM.format(SPUtils.getInstance().getLong("xwsb")),
                            location = "天津西青区八维学校",
                            currentLocation = "天津西青区八维学校",
                            userId = 1,
                            id = 0
                        )
                    )
                    SPUtils.getInstance().put("Dk",false)
                    SPUtils.getInstance().put("time",System.currentTimeMillis())
                    return@setOnClickListener
                }
                if (SPUtils.getInstance().getBoolean("xiawu", true)) {
                    mBinding.tvDkBtn.text = "已下班"
                    SPUtils.getInstance().put("xiawu",false)
                    SPUtils.getInstance().put("xwsb",System.currentTimeMillis())
                    mBinding.btnDk.setBackgroundResource(R.drawable.shape_dk_bg_not)
                }
            }
        }


        mBinding.header.back.setOnClickListener {
            finish()
        }
        mBinding.header.history.setOnClickListener {
            ARouter.getInstance().build(RouterPath.CLOCKING_LIST_UI).navigation()
        }
    }

    override fun initListener() {
        super.initListener()
    }

}